package com.example.etwzgzxt.view.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.etwzgzxt.R;
import com.example.etwzgzxt.tool.HttpHelper;
import com.example.etwzgzxt.tool.InfoApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

public class LoginActivity extends AppCompatActivity {

    private View.OnClickListener navListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginActivity.this.finish();
        }
    };

    private EditText etPNum, etPwd;
    private ProgressDialog progressDialog;
    private BackgroundTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("登录");
            setSupportActionBar(toolbar);
            toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow_back);
            toolbar.setNavigationOnClickListener(navListener);
        }
        initView();
    }

    public void initView() {
        etPNum = (EditText) findViewById(R.id.editTextId);
        etPwd = (EditText) findViewById(R.id.editTextPassword);
    }

    public void btnPasswordForget(View view) {
        startActivity(new Intent(this, FindPwdActivity.class));
    }

    public void btnLogin(View view) {
        if (etPNum.getText().length() == 11 && etPwd.getText().length() >= 6) {
            progressDialog = ProgressDialog.show(this, null, "登录中，请稍后……");
            task = new BackgroundTask();
            task.execute();
        }
    }

    public void btnRegister(View view) {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    public void btnJustLook(View view) {
        finish();
    }

    public class BackgroundTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            return login();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            parseResult(result);
        }
    }

    public String login() {
        String baseURL =((InfoApplication)getApplicationContext()).getBasehost()+"/user/authorize.do";

        NameValuePair pair = new BasicNameValuePair("phonenumber", etPNum.getText().toString());
        NameValuePair pair1 = new BasicNameValuePair("password", etPwd.getText().toString());
        List<NameValuePair> pairList = new ArrayList<>();
        pairList.add(pair);
        pairList.add(pair1);
        return new HttpHelper().post(baseURL, pairList);
    }

    public void parseResult(String result) {
        progressDialog.dismiss();
        if (result == null) {
            Toast.makeText(this, "请检查网络设置", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            switch (jsonObject.getInt("code")) {
                case 200:
                    InfoApplication info = (InfoApplication) getApplication();
                    info.setAccess_token(jsonObject.getString("access_token"));
                    finish();
                    return;
                case 403:
                    Toast.makeText(this, "Not Acceptable", Toast.LENGTH_SHORT).show();
                    break;
                case 406:
                    Toast.makeText(this, "Forbidden", Toast.LENGTH_SHORT).show();
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
